package com.andromeda.tutorial;

import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ViewSwitcher.ViewFactory;

public class ImgSwitcherActivity extends Activity implements ViewFactory {
	
	private int curIndex;
	private int index;
	private int downX, upX;
	private Field[] fields;
	private ImageSwitcher is;
	private ArrayList<Integer> imgList;
	private ArrayList<Drawable> imgDrawList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.dialog);
		
		curIndex=0;
		imgList = new ArrayList<Integer>();
		imgDrawList = new ArrayList<Drawable>();
		imgDrawList.add(getResources().getDrawable(R.raw.sayfa1));
		imgDrawList.add(getResources().getDrawable(R.raw.sayfa2));
		imgDrawList.add(getResources().getDrawable(R.raw.sayfa3));
		imgDrawList.add(getResources().getDrawable(R.raw.sayfa4));
		fields = R.raw.class.getDeclaredFields();
		for (Field field : fields) {

			
			try {
				index = field.getInt(R.raw.class);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			
			imgList.add(index);

		}

		is = (ImageSwitcher) findViewById(R.id.switcher);
		is.setFactory(this);
		is.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		is.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
		
		is.setImageDrawable(imgDrawList.get(curIndex));
//		is.setImageResource(imgList.get(curIndex));
		
		
		is.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {

				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					downX = (int) event.getX();
					return true;
				}

				else if (event.getAction() == MotionEvent.ACTION_UP) {
					
					upX = (int) event.getX();
					
					if (upX - downX > 100) { //To left

						// curIndex current image index in array viewed by user
						if(curIndex>0){
							curIndex--;
							is.setInAnimation(AnimationUtils.loadAnimation(
									ImgSwitcherActivity.this,
									android.R.anim.slide_in_left));
							is.setOutAnimation(AnimationUtils.loadAnimation(
									ImgSwitcherActivity.this,
									android.R.anim.slide_out_right));
							is.setImageDrawable(imgDrawList.get(curIndex));
//							is.setImageResource(imgList.get(curIndex));
						}
					}

					else if (downX - upX > 100) { // To right

						curIndex++;
						if (curIndex > imgList.size() - 1) {
//							is.setInAnimation(AnimationUtils.loadAnimation(ImgSwitcherActivity.this, 
//									R.anim.slide_in_right));
//							is.setOutAnimation(AnimationUtils.loadAnimation(ImgSwitcherActivity.this, 
//									android.R.anim.fade_out));
							curIndex--;
							finish();
							startActivity(new Intent(ImgSwitcherActivity.this,
									TutorialDeneme.class));
						}

						is.setInAnimation(AnimationUtils.loadAnimation(
								ImgSwitcherActivity.this,
								R.anim.slide_in_right));
						is.setOutAnimation(AnimationUtils.loadAnimation(
								ImgSwitcherActivity.this,
								R.anim.slide_out_left));
						is.setImageDrawable(imgDrawList.get(curIndex));
//						is.setImageResource(imgList.get(curIndex));
					}
					return true;
				}
				return false;
			}
		});
	}

	public View makeView() {
		ImageView i = new ImageView(this);
		i.setBackgroundColor(0xFF000000);
		i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		i.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return i;
	}

	public class ImageAdapter extends BaseAdapter {
		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return imgList.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);
			i.setImageResource(imgList.get(position));
			i.setScaleType(ScaleType.FIT_START);
			i.setAdjustViewBounds(true);
			i.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			return i;
		}

		private Context mContext;
	}
}